/**
 * @author zhangxiaofei [<1326336909@qq.com>]
 * @dateTime 2023-02-12
 *
 * 数据表格钩子
 */
import { useBase } from "@/hooks/base/useBase"
import { useStore } from "@/hooks/store/useStore"

export const useTable = (model, dataForm) => {

  const { i18n } = useBase()
  const { base } = useStore()

  const refresh = inject("refresh")

  const formRef = ref()

  const dataList = ref([])
  const pageIndex = ref(1)
  const pageSize = ref(10)
  const totalPage = ref(0)
  const dataListLoading = ref(false)
  const dataListSelections = ref([])


  /**
   * @author zhangxiaofei [<1326336909@qq.com>]
   * @dateTime 2023-02-10
   *
   * 设置每页显示条数
   *
   * @param {[type]} size 显示条数
   * @return {[type]}
   */
  const sizeChangeHandle = (size) => {
    pageSize.value = size
    pageIndex.value = 1
    getDataList()
  }


  /**
   * @author zhangxiaofei [<1326336909@qq.com>]
   * @dateTime 2023-02-10
   *
   * 设置当前页数
   *
   * @param {[type]} size 当前页数
   * @return {[type]}
   */
  const currentChangeHandle = (size) => {
    pageIndex.value = size
    base.setModel(model)
    base.setPage(size)
    getDataList()
  }

  /**
   * @author zhangxiaofei [<1326336909@qq.com>]
   * @dateTime 2023-02-10
   *
   * 多选函数
   *
   * @param {[type]} val 行号
   * @return {[type]}
   */
  const selectionChangeHandle = (val) => {
    dataListSelections.value = val
  }

  /**
   * @author zhangxiaofei [<1326336909@qq.com>]
   * @dateTime 2023-02-10
   *
   * 获取数据列表
   *
   * @param {[type]} is_search 是否为搜索操作
   * @return {[type]}
   */
  const getDataList = (is_search) => {
    is_search = is_search || false

    if (is_search) {
      pageIndex.value = 1
    }

    if(model == base.model) {
      pageIndex.value = base.page
    } else {
      base.setPage(1)
    }

    dataListLoading.value = true

    $http({
      url: $http.adornUrl('/' + model + '/list'),
      method: 'get',
      params: $http.adornParams({
        is_search: is_search.value,
        page: pageIndex.value,
        limit: pageSize.value,
        name: dataForm.name,
        head: dataForm.head,
        type: dataForm.type,
        title: dataForm.title,
        sex: dataForm.sex,
        code: dataForm.code,
        level: dataForm.level,
        email: dataForm.email,
        mobile: dataForm.mobile,
        status: dataForm.status,
        keyword: dataForm.keyword,
        username: dataForm.username,
        nickname: dataForm.nickname,
        realname: dataForm.realname,
        id_card_no: dataForm.id_card_no,
        content: dataForm.content,
        category: dataForm.category,
        browser: dataForm.browser,
        system: dataForm.system,
        address: dataForm.address,
        pay_type: dataForm.pay_type,
        order_no: dataForm.order_no,

        member_role_id: dataForm.member_role_id,
        member_username: dataForm.member_username,
        member_nickname: dataForm.member_nickname,

        id: dataForm.id,
        pid: dataForm.pid,
        user_id: dataForm.user_id,
        role_id: dataForm.role_id,
        unit_id: dataForm.unit_id,
        parent_id: dataForm.parent_id,
        member_id: dataForm.member_id,
        category_id: dataForm.category_id,
        location_id: dataForm.location_id,
        dictionary_id: dataForm.dictionary_id,
        organization_id: dataForm.organization_id,
        region_id: dataForm.region_id,
        nation_id: dataForm.nation_id,
        shipowner_id: dataForm.shipowner_id,
        shiproute_id: dataForm.shiproute_id,
        manager_id: dataForm.manager_id,
        machine_id: dataForm.machine_id,
        import_id: dataForm.import_id,
        nation_id: dataForm.nation_id,
        inviter_id: dataForm.inviter_id,
        contract_id: dataForm.contract_id,
        inquiry_id: dataForm.inquiry_id,

        pay_status: dataForm.pay_status,
        sign_status: dataForm.sign_status,
        audit_status: dataForm.audit_status,
        order_status: dataForm.order_status,
        market_status: dataForm.market_status,
        verify_status: dataForm.verify_status,
        article_status: dataForm.article_status,
        inquiry_status: dataForm.inquiry_status,
        certification_status: dataForm.certification_status,

        is_open: dataForm.is_open,
        is_hidden: dataForm.is_hidden,
        is_recommend: dataForm.is_recommend,
        is_visitor: dataForm.is_visitor,
        is_real: dataForm.is_real,
        is_grant: dataForm.is_grant,
        is_use: dataForm.is_use,
        scac: dataForm.scac,
        mmsi: dataForm.mmsi,
        vessel: dataForm.vessel,
        etd: dataForm.etd,
        language: dataForm.language,
        ship_name: dataForm.ship_name,

        add_time: dataForm.add_time,
        pay_time: dataForm.pay_time,
        valid_time: dataForm.valid_time,
        create_time: dataForm.create_time,
        update_time: dataForm.update_time,
        search_date: dataForm.search_date,
        last_login_time: dataForm.last_login_time,

        archive_contact: dataForm.contact,
        archive_contact_mobile: dataForm.contact_mobile,
        alpah_2_code: dataForm.alpah_2_code,
        company_name: dataForm.company_name,
        contact_name: dataForm.contact_name,

        chinese_name: dataForm.chinese_name,
        english_name: dataForm.english_name,
        chinese_title: dataForm.chinese_title,
        english_title: dataForm.english_title,
        publish_time: dataForm.publish_time,
        harbor_code: dataForm.harbor_code,
        start_harbor_code: dataForm.start_harbor_code,
        end_harbor_code: dataForm.end_harbor_code,
        inquiry_publish: dataForm.inquiry_publish,
        inquiry_receive: dataForm.inquiry_receive,
        schedule_publish: dataForm.schedule_publish,


        article__title: dataForm.article__title,
        member__username: dataForm.member__username,
        member__nickname: dataForm.member__nickname,
        demand__demand_type: dataForm.demand__demand_type,
        place__title: dataForm.place__title,
        category__category_id: dataForm.category__category_id,
        manager__manager_id: dataForm.manager__manager_id,
        harbor__chinese_name: dataForm.harbor__chinese_name,
        harbor__english_name: dataForm.harbor__english_name,
        member__chinese_nickname: dataForm.member__chinese_nickname,
        consignor__chinese_title: dataForm.consignor__chinese_title,
        transporter__chinese_title: dataForm.transporter__chinese_title,
        transporterMember__username: dataForm.transporterMember__username,
        organization__chinese_title: dataForm.organization__chinese_title,
        inquiry__description: dataForm.inquiry__description,
      }),
    }).then(({ data }) => {
      if (data && data.status === 200) {
        dataList.value = data.data.data
        totalPage.value = data.data.total
      } else {
        dataList.value.length = 0
        totalPage.value = 0
        $message.error(data.message)
      }
      dataListLoading.value = false
    })
  }

  // 改变状态
  const handleStatus = ($event, id, field) => {
    $http({
      url: $http.adornUrl('/' + model + '/status'),
      method: 'post',
      data: {
        id: id,
        field: field,
        value: $event
      },
    }).then(({ data }) => {
      if (data && data.status === 200) {
        getDataList()
      } else {
        $message.error(data.message)
      }
    })
  }

  // 改变排序
  const handleSort = ($event, id) => {
    $http({
      url: $http.adornUrl('/' + model + '/sort'),
      method: 'post',
      data: {
        id: id,
        sort: $event
      },
    }).then(({ data }) => {
      if (data && data.status === 200) {
        getDataList()
      } else {
        $message.error(data.message)
      }
    })
  }


  // 导出
  const handleExport = () => {
    $http({
      url: $http.adornUrl('/' + model + '/export'),
      method: 'post',
      data: $http.adornData({
        id: dataForm.id,
      }),
    }).then(({ data }) => {
      if (data && data.status === 200) {
        window.open(data.data)
      } else {
        $message.error(data.message)
      }
    })
  }


  // 删除
  const deleteHandle = (id) => {
    var ids = id ? [id] : dataListSelections.value.map((item) => {return item.id})

    let message = i18n('common.to_determine_the') +
                  i18n('common.current_check') +
                  i18n('common.for') +
                  `${id ? i18n('common.delete') : i18n('common.batch_delete')}` +
                  i18n('common.handle') + `?`

    $messagebox.confirm(message, i18n('common.prompt'), {
      confirmButtonText: i18n('common.confirm'),
      cancelButtonText: i18n('common.cancel'),
      type: 'warning',
    }).then(() => {
      $http({
        url: $http.adornUrl('/' + model + '/delete'),
        method: 'post',
        data: { id: $http.adornData(ids, false) },
      }).then(({ data }) => {
        if (data && data.status === 200) {
          $message({
            message: i18n('common.handle_success'),
            type: 'success',
            duration: 1500,
            onClose: () => {
              // getDataList()
              refresh()
            },
          })
        } else {
          $message.error(data.message)
        }
      })
    }).catch(() => {})
  }

  return {
    dataList,
    pageIndex,
    pageSize,
    totalPage,
    dataListLoading,
    dataListSelections,
    sizeChangeHandle,
    currentChangeHandle,
    selectionChangeHandle,
    getDataList,
    handleSort,
    handleStatus,
    deleteHandle,
  }
}
